package com.ld.control.common.equipment.method;

import com.alibaba.fastjson.JSONObject;
import com.ld.control.business.process.model.ProcessNodeExampleRunningParamModel;
import com.ld.control.business.process.model.ProcessNodeParamExampleModel;
import com.ld.control.business.robot.model.RobotPointModel;
import com.ld.control.business.robot.service.RobotPointService;
import com.ld.control.business.robot.vo.resp.RobotPointRespVo;
import com.ld.control.business.variable.service.VariableExampleService;
import com.ld.control.business.variable.vo.resp.VariableExampleValueResp;
import com.ld.control.common.equipment.param.OBRobotMethodParamConstant;
import com.ld.control.common.vo.EquipmentSystemMethodVo;
import com.ld.control.exception.BusinessException;
import com.ld.control.sysenum.MethodParamTypeEnum;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Slf4j
@Data
@Component
public class OBRobotEquipmentSystemMethod {

    @Autowired
    private RobotPointService robotPointService;

    @Autowired
    private VariableExampleService variableExampleService;


    public EquipmentSystemMethodVo get_Pick_OB_Ext(Long processNodeExampleRunningModelId, Map<String,
            ProcessNodeParamExampleModel> processNodeParamExampleModeMap) {
        EquipmentSystemMethodVo equipmentSystemMethodVo = new EquipmentSystemMethodVo();
        List<ProcessNodeExampleRunningParamModel> processNodeExampleRunningParamModelList = new ArrayList<>();

        ProcessNodeParamExampleModel initDistanceValue = processNodeParamExampleModeMap.get("initDistance");
        ProcessNodeParamExampleModel jawFlagValue = processNodeParamExampleModeMap.get("jawFlag");
        ProcessNodeParamExampleModel jawDistanceValue = processNodeParamExampleModeMap.get("jawDistance");
        ProcessNodeParamExampleModel jawForceLimitValue = processNodeParamExampleModeMap.get("jawForceLimit");

        RobotPointRespVo robotPointInfo = getRobotPointInfo(processNodeParamExampleModeMap);
        log.info("get_Pick_OB_Ext_robotPointInfo:{}", JSONObject.toJSONString(robotPointInfo));

        ProcessNodeExampleRunningParamModel targetPointParam = new ProcessNodeExampleRunningParamModel();
        targetPointParam.setProcessNodeExampleRunningId(processNodeExampleRunningModelId);
        targetPointParam.setSendMethodParamName(OBRobotMethodParamConstant.Pick.targetPoint);
        targetPointParam.setSendMethodParamValue(robotPointInfo.getTargetPoint());
        targetPointParam.setParamType(MethodParamTypeEnum.STRING.getType());

        ProcessNodeExampleRunningParamModel targetSafePointParam = new ProcessNodeExampleRunningParamModel();
        targetSafePointParam.setProcessNodeExampleRunningId(processNodeExampleRunningModelId);
        targetSafePointParam.setSendMethodParamName(OBRobotMethodParamConstant.Pick.targetSafePoint);
        targetSafePointParam.setSendMethodParamValue(robotPointInfo.getInitPoint());
        targetSafePointParam.setParamType(MethodParamTypeEnum.STRING.getType());

        ProcessNodeExampleRunningParamModel jawDistanceParam = new ProcessNodeExampleRunningParamModel();
        jawDistanceParam.setProcessNodeExampleRunningId(processNodeExampleRunningModelId);
        jawDistanceParam.setSendMethodParamName(OBRobotMethodParamConstant.Pick.jawDistance);
        jawDistanceParam.setSendMethodParamValue(String.valueOf(jawDistanceValue.getValue()));
        jawDistanceParam.setParamType(MethodParamTypeEnum.STRING.getType());

        ProcessNodeExampleRunningParamModel jawForceLimitParam = new ProcessNodeExampleRunningParamModel();
        jawForceLimitParam.setProcessNodeExampleRunningId(processNodeExampleRunningModelId);
        jawForceLimitParam.setSendMethodParamName(OBRobotMethodParamConstant.Pick.jawForceLimit);
        jawForceLimitParam.setSendMethodParamValue(String.valueOf(jawForceLimitValue.getValue()));
        jawForceLimitParam.setParamType(MethodParamTypeEnum.STRING.getType());

        ProcessNodeExampleRunningParamModel initDistanceParam = new ProcessNodeExampleRunningParamModel();
        initDistanceParam.setProcessNodeExampleRunningId(processNodeExampleRunningModelId);
        initDistanceParam.setSendMethodParamName(OBRobotMethodParamConstant.Pick.initDistance);
        initDistanceParam.setSendMethodParamValue(initDistanceValue.getValue());
        initDistanceParam.setParamType(MethodParamTypeEnum.STRING.getType());

        ProcessNodeExampleRunningParamModel jawFlagParam = new ProcessNodeExampleRunningParamModel();
        jawFlagParam.setProcessNodeExampleRunningId(processNodeExampleRunningModelId);
        jawFlagParam.setSendMethodParamName(OBRobotMethodParamConstant.Pick.jawFlag);
        jawFlagParam.setSendMethodParamValue(jawFlagValue == null ? "0" : jawFlagValue.getValue());
        jawFlagParam.setParamType(MethodParamTypeEnum.STRING.getType());

        //数据加入到集合
        processNodeExampleRunningParamModelList.add(targetPointParam);
        processNodeExampleRunningParamModelList.add(targetSafePointParam);
        processNodeExampleRunningParamModelList.add(jawDistanceParam);
        processNodeExampleRunningParamModelList.add(jawForceLimitParam);
        processNodeExampleRunningParamModelList.add(initDistanceParam);
        processNodeExampleRunningParamModelList.add(jawFlagParam);
        equipmentSystemMethodVo.setProcessNodeExampleRunningParamModelList(processNodeExampleRunningParamModelList);
        equipmentSystemMethodVo.setEquipmentMethodName(OBRobotMethodParamConstant.Pick.methodName);
        return equipmentSystemMethodVo;
    }


    public EquipmentSystemMethodVo get_Place_OB_Ext(Long processNodeExampleRunningModelId, Map<String, ProcessNodeParamExampleModel> processNodeParamExampleModeMap) {
        EquipmentSystemMethodVo equipmentSystemMethodVo = new EquipmentSystemMethodVo();
        List<ProcessNodeExampleRunningParamModel> processNodeExampleRunningParamModelList = new ArrayList<>();
        ProcessNodeParamExampleModel initDistanceValue = processNodeParamExampleModeMap.get("initDistance");
        ProcessNodeParamExampleModel jawFlagValue = processNodeParamExampleModeMap.get("jawFlag");

        RobotPointRespVo robotPointInfo = getRobotPointInfo(processNodeParamExampleModeMap);
        log.info("get_Place_OB_Ext_robotPointInfo:{}", JSONObject.toJSONString(robotPointInfo));

        ProcessNodeExampleRunningParamModel targetPoint = new ProcessNodeExampleRunningParamModel();
        targetPoint.setProcessNodeExampleRunningId(processNodeExampleRunningModelId);
        targetPoint.setSendMethodParamName(OBRobotMethodParamConstant.Place.targetPoint);
        targetPoint.setSendMethodParamValue(robotPointInfo.getTargetPoint());
        targetPoint.setParamType(MethodParamTypeEnum.STRING.getType());

        ProcessNodeExampleRunningParamModel targetSafePoint = new ProcessNodeExampleRunningParamModel();
        targetSafePoint.setProcessNodeExampleRunningId(processNodeExampleRunningModelId);
        targetSafePoint.setSendMethodParamName(OBRobotMethodParamConstant.Place.targetSafePoint);
        targetSafePoint.setSendMethodParamValue(robotPointInfo.getInitPoint());
        targetSafePoint.setParamType(MethodParamTypeEnum.STRING.getType());

        ProcessNodeExampleRunningParamModel initDistanceParam = new ProcessNodeExampleRunningParamModel();
        initDistanceParam.setProcessNodeExampleRunningId(processNodeExampleRunningModelId);
        initDistanceParam.setSendMethodParamName(OBRobotMethodParamConstant.Place.initDistance);
        initDistanceParam.setSendMethodParamValue(initDistanceValue.getValue());
        initDistanceParam.setParamType(MethodParamTypeEnum.STRING.getType());

        ProcessNodeExampleRunningParamModel jawFlagParam = new ProcessNodeExampleRunningParamModel();
        jawFlagParam.setProcessNodeExampleRunningId(processNodeExampleRunningModelId);
        jawFlagParam.setSendMethodParamName(OBRobotMethodParamConstant.Pick.jawFlag);
        jawFlagParam.setSendMethodParamValue(jawFlagValue == null ? "0" : jawFlagValue.getValue());
        jawFlagParam.setParamType(MethodParamTypeEnum.STRING.getType());


        //数据加入到集合
        processNodeExampleRunningParamModelList.add(targetPoint);
        processNodeExampleRunningParamModelList.add(targetSafePoint);
        processNodeExampleRunningParamModelList.add(initDistanceParam);
        processNodeExampleRunningParamModelList.add(jawFlagParam);
        equipmentSystemMethodVo.setProcessNodeExampleRunningParamModelList(processNodeExampleRunningParamModelList);
        equipmentSystemMethodVo.setEquipmentMethodName(OBRobotMethodParamConstant.Place.methodName);
        return equipmentSystemMethodVo;
    }


    public EquipmentSystemMethodVo get_SingleAction_OB_Ext(Long processNodeExampleRunningModelId, Map<String, ProcessNodeParamExampleModel> processNodeParamExampleModeMap) {
        EquipmentSystemMethodVo equipmentSystemMethodVo = new EquipmentSystemMethodVo();
        List<ProcessNodeExampleRunningParamModel> processNodeExampleRunningParamModelList = new ArrayList<>();

        ProcessNodeParamExampleModel initDistanceValue = processNodeParamExampleModeMap.get("initDistance");
        ProcessNodeParamExampleModel jawFlagValue = processNodeParamExampleModeMap.get(".");

        RobotPointRespVo robotPointInfo = getRobotPointInfo(processNodeParamExampleModeMap);
        log.info("get_SingleAction_OB_Ext_robotPointInfo:{}", JSONObject.toJSONString(robotPointInfo));

        ProcessNodeExampleRunningParamModel targetPoint = new ProcessNodeExampleRunningParamModel();
        targetPoint.setProcessNodeExampleRunningId(processNodeExampleRunningModelId);
        targetPoint.setSendMethodParamName(OBRobotMethodParamConstant.SingleAction.targetPoint);
        targetPoint.setSendMethodParamValue(robotPointInfo.getTargetPoint());
        targetPoint.setParamType(MethodParamTypeEnum.STRING.getType());

        ProcessNodeExampleRunningParamModel initDistance = new ProcessNodeExampleRunningParamModel();
        initDistance.setProcessNodeExampleRunningId(processNodeExampleRunningModelId);
        initDistance.setSendMethodParamName(OBRobotMethodParamConstant.SingleAction.initDistance);
        initDistance.setSendMethodParamValue(initDistanceValue.getValue());
        initDistance.setParamType(MethodParamTypeEnum.STRING.getType());

        ProcessNodeExampleRunningParamModel jawFlagParam = new ProcessNodeExampleRunningParamModel();
        jawFlagParam.setProcessNodeExampleRunningId(processNodeExampleRunningModelId);
        jawFlagParam.setSendMethodParamName(OBRobotMethodParamConstant.Pick.jawFlag);
        jawFlagParam.setSendMethodParamValue(jawFlagValue == null ? "0" : jawFlagValue.getValue());
        jawFlagParam.setParamType(MethodParamTypeEnum.STRING.getType());

        //数据加入到集合
        processNodeExampleRunningParamModelList.add(targetPoint);
        processNodeExampleRunningParamModelList.add(initDistance);
        processNodeExampleRunningParamModelList.add(jawFlagParam);
        equipmentSystemMethodVo.setProcessNodeExampleRunningParamModelList(processNodeExampleRunningParamModelList);
        equipmentSystemMethodVo.setEquipmentMethodName(OBRobotMethodParamConstant.SingleAction.methodName);
        return equipmentSystemMethodVo;
    }

    public EquipmentSystemMethodVo get_DoubleAction_OB_Ext(Long processNodeExampleRunningModelId, Map<String, ProcessNodeParamExampleModel> processNodeParamExampleModeMap) {
        EquipmentSystemMethodVo equipmentSystemMethodVo = new EquipmentSystemMethodVo();
        List<ProcessNodeExampleRunningParamModel> processNodeExampleRunningParamModelList = new ArrayList<>();

        ProcessNodeParamExampleModel initDistanceValue = processNodeParamExampleModeMap.get("initDistance");
        ProcessNodeParamExampleModel jawFlagValue = processNodeParamExampleModeMap.get("jawFlag");


        RobotPointRespVo robotPointInfo = getRobotPointInfo(processNodeParamExampleModeMap);
        log.info("get_DoubleAction_OB_Ext_robotPointInfo:{}", JSONObject.toJSONString(robotPointInfo));

        ProcessNodeExampleRunningParamModel targetPoint = new ProcessNodeExampleRunningParamModel();
        targetPoint.setProcessNodeExampleRunningId(processNodeExampleRunningModelId);
        targetPoint.setSendMethodParamName(OBRobotMethodParamConstant.DoubleAction.targetPoint);
        targetPoint.setSendMethodParamValue(robotPointInfo.getTargetPoint());
        targetPoint.setParamType(MethodParamTypeEnum.STRING.getType());

        ProcessNodeExampleRunningParamModel targetSafePoint = new ProcessNodeExampleRunningParamModel();
        targetSafePoint.setProcessNodeExampleRunningId(processNodeExampleRunningModelId);
        targetSafePoint.setSendMethodParamName(OBRobotMethodParamConstant.DoubleAction.targetSafePoint);
        targetSafePoint.setSendMethodParamValue(robotPointInfo.getInitPoint());
        targetSafePoint.setParamType(MethodParamTypeEnum.STRING.getType());

        ProcessNodeExampleRunningParamModel initDistanceParam = new ProcessNodeExampleRunningParamModel();
        initDistanceParam.setProcessNodeExampleRunningId(processNodeExampleRunningModelId);
        initDistanceParam.setSendMethodParamName(OBRobotMethodParamConstant.DoubleAction.initDistance);
        initDistanceParam.setSendMethodParamValue(initDistanceValue.getValue());
        initDistanceParam.setParamType(MethodParamTypeEnum.STRING.getType());

        ProcessNodeExampleRunningParamModel jawFlagParam = new ProcessNodeExampleRunningParamModel();
        jawFlagParam.setProcessNodeExampleRunningId(processNodeExampleRunningModelId);
        jawFlagParam.setSendMethodParamName(OBRobotMethodParamConstant.Pick.jawFlag);
        jawFlagParam.setSendMethodParamValue(jawFlagValue == null ? "0" : jawFlagValue.getValue());
        jawFlagParam.setParamType(MethodParamTypeEnum.STRING.getType());

        //数据加入到集合
        processNodeExampleRunningParamModelList.add(targetPoint);
        processNodeExampleRunningParamModelList.add(targetSafePoint);
        processNodeExampleRunningParamModelList.add(initDistanceParam);
        processNodeExampleRunningParamModelList.add(jawFlagParam);
        equipmentSystemMethodVo.setProcessNodeExampleRunningParamModelList(processNodeExampleRunningParamModelList);
        equipmentSystemMethodVo.setEquipmentMethodName(OBRobotMethodParamConstant.DoubleAction.methodName);
        return equipmentSystemMethodVo;
    }


    public EquipmentSystemMethodVo get_Agv_Move_OB_Ext(Long processNodeExampleRunningModelId, Map<String, ProcessNodeParamExampleModel> processNodeParamExampleModeMap) {
        EquipmentSystemMethodVo equipmentSystemMethodVo = new EquipmentSystemMethodVo();
        List<ProcessNodeExampleRunningParamModel> processNodeExampleRunningParamModelList = new ArrayList<>();

        ProcessNodeParamExampleModel timeoutValue = processNodeParamExampleModeMap.get("timeout");
        RobotPointRespVo robotPointInfo = getRobotPointInfo(processNodeParamExampleModeMap);
        log.info("get_Agv_Move_OB_Ext_robotPointInfo:{}", JSONObject.toJSONString(robotPointInfo));

        ProcessNodeExampleRunningParamModel targetPosParam = new ProcessNodeExampleRunningParamModel();
        targetPosParam.setProcessNodeExampleRunningId(processNodeExampleRunningModelId);
        targetPosParam.setSendMethodParamName(OBRobotMethodParamConstant.Agv_Move.targetPos);
        targetPosParam.setSendMethodParamValue(robotPointInfo.getAgvPosition());
        targetPosParam.setParamType(MethodParamTypeEnum.STRING.getType());

        ProcessNodeExampleRunningParamModel timeoutParam = new ProcessNodeExampleRunningParamModel();
        timeoutParam.setProcessNodeExampleRunningId(processNodeExampleRunningModelId);
        timeoutParam.setSendMethodParamName(OBRobotMethodParamConstant.Agv_Move.timeout);
        timeoutParam.setSendMethodParamValue(timeoutValue.getValue());
        timeoutParam.setParamType(MethodParamTypeEnum.STRING.getType());

        //数据加入到集合
        processNodeExampleRunningParamModelList.add(targetPosParam);
        processNodeExampleRunningParamModelList.add(timeoutParam);
        equipmentSystemMethodVo.setProcessNodeExampleRunningParamModelList(processNodeExampleRunningParamModelList);
        equipmentSystemMethodVo.setEquipmentMethodName(OBRobotMethodParamConstant.Agv_Move.methodName);
        return equipmentSystemMethodVo;
    }


    public EquipmentSystemMethodVo get_Track_Move_Ext(Long processNodeExampleRunningModelId, Map<String, ProcessNodeParamExampleModel> processNodeParamExampleModeMap) {
        EquipmentSystemMethodVo equipmentSystemMethodVo = new EquipmentSystemMethodVo();
        List<ProcessNodeExampleRunningParamModel> processNodeExampleRunningParamModelList = new ArrayList<>();

        RobotPointRespVo robotPointInfo = getRobotPointInfo(processNodeParamExampleModeMap);
        log.info("get_Track_Move_Ext_robotPointInfo:{}", JSONObject.toJSONString(robotPointInfo));

        ProcessNodeExampleRunningParamModel distance = new ProcessNodeExampleRunningParamModel();
        distance.setProcessNodeExampleRunningId(processNodeExampleRunningModelId);
        distance.setSendMethodParamName(OBRobotMethodParamConstant.Track_Move.distance);
        distance.setSendMethodParamValue(robotPointInfo.getTrackPosition());
        distance.setParamType(MethodParamTypeEnum.STRING.getType());
        //数据加入到集合
        processNodeExampleRunningParamModelList.add(distance);
        equipmentSystemMethodVo.setProcessNodeExampleRunningParamModelList(processNodeExampleRunningParamModelList);
        equipmentSystemMethodVo.setEquipmentMethodName(OBRobotMethodParamConstant.Track_Move.methodName);
        return equipmentSystemMethodVo;
    }


    private RobotPointRespVo getRobotPointInfo(Map<String, ProcessNodeParamExampleModel> processNodeParamExampleModeMap) {
        // 傲博机械臂
        ProcessNodeParamExampleModel processNodeParamExampleModel = processNodeParamExampleModeMap.get("robotPointName");
        String robotPointName = processNodeParamExampleModel.getValue();
        Long processNodeExampleId = processNodeParamExampleModel.getProcessNodeExampleId();
        if (robotPointName.startsWith("@")) {
            VariableExampleValueResp byNameExt = variableExampleService.getByNameExt(processNodeExampleId, robotPointName);
            robotPointName = byNameExt.getCurrentValue();
        }
        // 获取点位信息
        RobotPointRespVo robotPointRespVo = robotPointService.getByName(robotPointName);
        if (robotPointRespVo == null) {
            throw new BusinessException(robotPointName + "点位不存在！");
        }
        return robotPointRespVo;
    }

}
